package start;

import com.oldking.checklogin.Check;
import com.oldking.controller.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

@Component
public class StarterClass extends Application {

    private static final int MAIN_VIEW_WIDTH = 1160;
    private static final int MAIN_VIEW_HEIGHT = 741;
    private static final double MINIMUM_WINDOW_WIDTH = 460.0;
    private static final double MINIMUM_WINDOW_HEIGHT = 290.0;

    private Stage stage;
    private ConfigurableApplicationContext applicationContext;


    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        stage.setTitle("小王视频编辑");
        stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
        stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
        stage.setResizable(false);
        gotologin();
        stage.show();
    }

    public void gotologin() {
        try {
            LoginController login = (LoginController) replaceSceneContent("/static/view/FXML_LOGIN.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(StarterClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotomain() {
        try {
            MainController main = (MainController) replaceSceneContent("/static/view/FXML_MAIN.fxml");
            stage.setWidth(MAIN_VIEW_WIDTH);
            stage.setHeight(MAIN_VIEW_HEIGHT);
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(StarterClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotoeditvideo(String item) {
        try {
            EditVideoController main = (EditVideoController) replaceSceneContent("/static/view/FXML_EDIT_VIDEO.fxml");
            stage.setWidth(MAIN_VIEW_WIDTH);
            stage.setHeight(MAIN_VIEW_HEIGHT);
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(StarterClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void gotoaudiomanage(String item) {
        try {
            MediaManageController main = (MediaManageController) replaceSceneContent("/static/view/FXML_MANAGE_MEDIA.fxml");
            stage.setWidth(MAIN_VIEW_WIDTH);
            stage.setHeight(MAIN_VIEW_HEIGHT);
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(StarterClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotoaddaudio(String item) {
        try {
            AddAudioController main = (AddAudioController) replaceSceneContent("/static/view/FXML_ADD_AUDIO.fxml");
            stage.setWidth(MAIN_VIEW_WIDTH);
            stage.setHeight(MAIN_VIEW_HEIGHT);
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(StarterClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void userlogin(String account, String password) {
        if (Check.checkreturn(account, password)) {
            gotomain();
        }
    }

    public void useroutmain() {
        gotologin();
    }

    private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = StarterClass.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(StarterClass.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 560, 320);
        stage.setScene(scene);
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }

//    @Override
//    public void init() {
//        applicationContext = new SpringApplicationBuilder(StartJavaFxApplication.class).run();
//    }

    @Override
    public void stop() {
    //    applicationContext.close();
        Platform.exit();
    }

    static class StageReadyEvent extends ApplicationEvent {
        public StageReadyEvent(Stage stage) {
            super(stage);
        }

        public Stage getStage() {
            return ((Stage) getSource());
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}